
const modules = import.meta.glob('../views/**/*.vue')

// 将菜单数据转换为路由对象
export function generateDynamicRoutes(menuList) {
    return menuList.map(menu => {
        const route = {
            //需要判断一下 有没有 / ,没有要加上
            path: menu.path.startsWith('/') ? menu.path : `/${menu.path}`,
            name: menu.name,
            component:modules[`../views/${capitalizeFirstLetter(menu.component)}.vue`],
            meta: {
                ...menu.meta,
                requiresAuth: true
            }
        }

        if (menu.children && menu.children.length > 0) {
            route.children = generateDynamicRoutes(menu.children)
        }

        return route
    })
}

/**
 * 定义一个方法，将路径中最后一个URL 的首字母转为大写.其次去掉开头的/
 */
function capitalizeFirstLetter(str) {
    // 去掉开头的 /
    str = str.replace(/^\//, '');
    const lastSlashIndex = str.lastIndexOf('/');
    if (lastSlashIndex !== -1) {
        const lastSegment = str.slice(lastSlashIndex + 1);
        const capitalizedLastSegment = lastSegment.charAt(0).toUpperCase() + lastSegment.slice(1);
        return str.slice(0, lastSlashIndex + 1) + capitalizedLastSegment;
    }
    return str;
}
